package com.common.utils;

import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import com.common.util.activity.BaseActivity;
import com.common.util.update.AppUpdateDialog;
import com.common.util.update.AppVersionInfoResBeen;
import com.common.util.update.Constant;
import com.common.util.utils.AppUtils;
import com.common.util.utils.ApplicationManager;
import com.common.util.utils.LogUtils;
import com.common.util.utils.ScreenUtils;

import java.io.File;

public class MainActivity extends BaseActivity {

    TextView tv_content;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        LogUtils.i("MainActivity", "onCreate");
        LogUtils.d("MainActivity", "onCreate");
        LogUtils.w("MainActivity", "onCreate");
        LogUtils.e("MainActivity", "onCreate");

        Context context = ApplicationManager.getInstance().getContext();

        LogUtils.e("MainActivity", "density=" + ScreenUtils.getDensity(context));
        LogUtils.e("MainActivity", "densityDpi=" + ScreenUtils.getDensityDpi(context));
        LogUtils.e("MainActivity", "screenWidth=" + ScreenUtils.getScreenWidth(context));
        LogUtils.e("MainActivity", "screenHeight=" + ScreenUtils.getScreenHeight(context));
        LogUtils.e("MainActivity", "statusBarHeight=" + ScreenUtils.getStatusBarHeight(context));
        LogUtils.e("MainActivity", "dip2px=" + ScreenUtils.dip2px(context, 10.0F));
        LogUtils.e("MainActivity", "px2dip=" + ScreenUtils.px2dip(context, 10.0F));

//        LogUtils.e("junit", DeviceUtils.buildSystemInfo(context));

        tv_content = findViewById(R.id.tv_content);
        tv_content.setText(AppUtils.getVersionName());
    }

    AppUpdateDialog updateDialog;

    public void doClick(View view){
        AppVersionInfoResBeen.DataBean dataBean = new AppVersionInfoResBeen.DataBean();
        switch (view.getId()) {
            case R.id.button1:
                dataBean.setType("02");
                break;
            case R.id.button2:
                dataBean.setType("01");
                break;
            case R.id.button3:
                if (hasPermission(Constant.WRITE_READ_EXTERNAL_PERMISSION)) {
                    doSDCardPermission();
                } else {
                    requestPermission(Constant.WRITE_READ_EXTERNAL_CODE, Constant.WRITE_READ_EXTERNAL_PERMISSION);
                }

                return;
//                break;
            case R.id.button4:
                tv_content.setText(AppUtils.getVersionName());
                break;
        }

        updateDialog = AppUpdateDialog.showUpdateDialog(this, dataBean, new AppUpdateDialog.AppUpgradeListener() {
            @Override
            public void update(String downUrl) {
                updateDialog.startDownload(downUrl);
            }

            @Override
            public void exitApp() {
//                finish();
            }
        });
    }

    @Override
    public void doSDCardPermission() {
        if (new File(Constant.filePath).exists()) {
            new AppUpdateDialog(this).installApk();
        }
    }
}
